<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            position: relative;
            width: 400px;
            border-bottom: 1px solid #ccc;
            margin: 100px auto;
        }

        .box input {
            width: 370px;
            height: 30px;
            border: 0;
            outline: none;
        }

        .box label {
            position: absolute;
            top: 2px;
            right: 2px;
            width: 24px;
            height: 24px;
            background-color: pink;
            cursor: pointer;
            background: url(./images/close.png) no-repeat;
            background-size: cover;
        }

        .box label.active {
            background-image: url(./images/open.png);
        }
    </style>
</head>

<body>
    <div class="box">
        <label for=""></label>
        <input type="password" name="" id="pwd">
    </div>
    <script>
        // 目标: 点击眼睛, 查看/隐藏
        // 1. 准备标签和样式-背景图(active类名)
        // 2. 获取标签
        let theLabel = document.querySelector('.box label')
        let myInput = document.querySelector('#pwd')
        // 3. 绑定label-点击事件
        theLabel.addEventListener('click', function(){
            // 4. 换label背景图(切换类名)
            theLabel.classList.toggle('active')
            // 5. 判断input类名 myInput.type (取类型字符串)
            if (myInput.type === 'text') {
                myInput.type = 'password'
            } else {
                myInput.type = 'text'
            }
        })
    </script>
</body>

</html>